<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use app\service\order\OrderService;
use think\Model;
use Workerman\Timer;

class GoodsOrder extends Model
{
	protected $autoWriteTimestamp = true;
	protected function getOptions(): array
	{
		return [
			'insert' => ['trade_no'],
		];
	}

	// 自动写入订单号及订单标识及倒计时关闭
	protected function setTradeNoAttr($value, $data)
	{
		$trade_no = generate_trade_no("T");
		OrderMaster::create(["trade_no" => $trade_no, "model" => 'GoodsOrder']);
		Timer::add(conf("order_auto_close_time") * 60, function () use ($trade_no) {
			$order = self::where([
				'trade_no' => $trade_no,
				'status'   => 0,
			])->find();
			if ($order) {
				$order->status = 2;
				$order->save();
			}
		});
		return $trade_no;
	}

	public function channel()
	{
		return $this->belongsTo("Channel", "channel_id");
	}

	public function channelAccount()
	{
		return $this->belongsTo("ChannelAccount", "channel_account_id");
	}

	public function goods()
	{
		return $this->belongsTo("Goods", "goods_id");
	}

	public function searchGoodsIdAttr($query, $value, $data)
	{
		$query->where("goods_id", $value);
	}

	public function searchPaytypeAttr($query, $value, $data)
	{
		$query->whereIn("paytype", $value);
	}
	public function searchStatusAttr($query, $value, $data)
	{
		$query->where("status", $value);
	}
	public function searchTradeNoAttr($query, $value, $data)
	{
		$query->where("trade_no", $value);
	}

	public function searchTransactionIdAttr($query, $value, $data)
	{
		$query->where("transaction_id", $value);
	}

	public function searchChannelIdAttr($query, $value, $data)
	{
		$query->where("channel_id", $value);
	}

	public function searchDateTypeAttr($query, $value, $data)
	{
		if ($value == 1) {
			$query->whereTime("create_time", "today");
		} elseif ($value == 2) {
			$query->whereTime("create_time", "yesterday");
		} elseif ($value == 3) {
			$query->whereTime("create_time", "week");
		} elseif ($value == 4) {
			$query->whereTime("create_time", "month");
		} elseif ($value == 5) {
			$query->whereTime("create_time", "year");
		}
	}
	public function searchDateRangeAttr($query, $value, $data)
	{
		if (empty($value) || empty(trim($value[0])) || empty(trim($value[1]))) {
			return;
		}
		if ($value[0] == $value[1]) {
			$query->whereTime('create_time', strtotime($value[0]));
			return;
		}
		$query->whereBetweenTime('create_time', strtotime($value[0]), strtotime($value[1]));
	}

	public function searchTypeAttr($query, $value, $data)
	{
		if (empty($data['keywords'])) {
			return;
		}
		if ($value == 'trade_no') {
			$query->where("trade_no", $data['keywords']);
		} elseif ($value == 'goods_name') {
			$query->where("goods_name", "like", "%" . $data['keywords'] . "%");
		}
	}

	public function searchCidAttr($query, $value, $data)
	{
		$query->hasWhere('goods', function ($query) use ($value) {
			$query->where('cate_id', $value);
		});
	}

	public function searchDateAttr($query, $value, $data)
	{
		$query->whereTime("create_time", "between", [$value . " 00:00:00", $value . " 23:59:59"]);
	}

	// 
	public function getStatusTextAttr($value, $data)
	{
		$arr = ["0" => "未支付", "1" => "已支付", "2" => "已关闭", "3" => "已退款"];
		return $arr[$data["status"]];
	}

	// 生成订单同步回调地址
	public function callbackUrl()
	{
		return 'order';
	}

	// 完成订单
	public function completeOrder($order)
	{
		(new OrderService())->complete($order);
	}
}
